<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

class FileEntryModel_dir extends FileEntryModel
{


	function sendFileDownload($data) {
		
		require_once 'HTTP/Download.php';
		PEAR::setErrorHandling(PEAR_ERROR_PRINT);
		
		session_write_close();
		ini_set('zlib.output_compression', 'Off');
		
		$params['file'] = $this->getFileDir($data);
		$params['gzip'] = true;
		
		$h = &new HTTP_Download($params);
        $h->setContentDisposition(HTTP_DOWNLOAD_ATTACHMENT, $data['filename']); //HTTP_DOWNLOAD_INLINE
		$h->send();
	}	
	
	
	

	// ACTIONS // ---------------------
	// we just delete old if update and file
	// file already saved by upload method in dir implementation
	function saveFileData($bin_data, $id = null) {
		
		//when update some file
		if($id) { $this->deleteFileData($id); }
		
		return $id;
	}
	
	
	function getFileContent($filename) {
		return true;
	}
	
	
	function upload() {
			
		require_once 'eleontev/Dir/Uploader.php';
	
		$upload = new Uploader;
		$upload->store_in_db = false;
		$upload->safe_name = true;
		$upload->setRenameValues('date');
		$upload->setAllowedExtension($this->setting['file_allowed_extensions']);
		$upload->setDeniedExtension($this->setting['file_denied_extensions']);
		$upload->setMaxSize($this->setting['file_max_filesize']);
		$upload->setUploadedDir($this->setting['file_dir']);
		
		$f = $upload->upload($_FILES);
	
		if(isset($f['bad'])) {
			$f['error_msg'] = $upload->errorBox($f['bad']);
		} else {
			$data['filename'] = $f['good'][1]['name'];
			$data['directory'] = $this->setting['file_dir'];
			$data['sub_directory'] = '';
			$f['good'][1]['to_read'] = $this->getFileDir($data);
			$f['good'][1]['directory'] = $this->setting['file_dir'];
		}

		return $f;
	}

	
	function readDirectory($dirname) {
		
		$d = &new MyDir;
		$d->one_level = true;
		$d->full_path = true;
		$d->setSkipDirs('.svn', 'cvs','.SVN', 'CVS', 'etc');
		$d->setAllowedExtension($this->setting['file_allowed_extensions']);
		$d->setDeniedExtension($this->setting['file_denied_extensions']);
		
		$dirname = str_replace('\\', '/', $dirname);
		$files = &$d->getFilesDirs($dirname);
		
		//echo "<pre>"; print_r($files); echo "</pre>";
		//exit;
		
		return $files;
	}
	
	
	function getFileData($file) {
		
		$d = &new MyDir;
		$data = array();
		
		$data['name'] = $d->getFilename($file);
		$data['type'] = mime_content_type($file);
		$data['tmp_name'] = '';
		$data['extension'] = $d->getFileExtension($file);
		$data['size'] = filesize($file);
		$data['to_read'] = $file;
		$data['directory'] = $d->getFileDirectory($file);
		
		return $data;
	}
	
	
	function save(&$obj, $action, $is_file) {
		
		if($action == 'insert') {
			$id = $this->add($obj);
			$this->saveEntryToCategory($obj, $id);
			
			if($this->isExtra('private')) {
				if($obj->get('private')) {
					$this->saveMemberRoleToEntry($obj, $id);
				}
			}
			
		} else {
		
			$id = $obj->get('id');
		
			// we have old filetext and no new filetext
			if(!$is_file) {
				unset($obj->properties['filetext']);
			} 
		
			$this->update($obj, $id);
			$this->deleteEntryToCategory($id);
			$this->saveEntryToCategory($obj, $id);
			
			if($this->isExtra('private')) {
				$this->deleteMemberRoleToEntry($id);
				if($obj->get('private')) {
					$this->saveMemberRoleToEntry($obj, $id);
				}					
			}			
		}
		
		return $id;
	}
	
	
	function getFileDir($data) {
		
		$files = array();
		$files[1] = $data['directory'] . '/' . $data['filename'];
		$files[2] = $this->setting['file_dir'] . $data['filename'];
		
		foreach($files as $file) {
			$file = str_replace('//', '/', $file);
			if(file_exists($file)) {
				return $file;
			}
		}
		
		return false;
	}	
	
	
	function getSetting($key) {
		return $this->setting[$key];
	}
	
	
	// DELETE RELATED // --------------------- 
	
	function deleteFileData($record_id, $from_disk) {
		if($from_disk) {
			unlink($this->getFileDir($this->getById($record_id)));
		}
	}
}
?>